#include "stdafx.h"
#include "CCheck.h"

NAMESPACE_CORE_BEGIN

static const UINT_32 RADIO_AUTOREPAINT = AutoRepaintMouseHoving | AutoRepaintMouseL | 
	AutoRepaintBackImageChanged;

static const INT_32 RADION_SPACE = 4;

CCheck::CCheck()
	: m_bChecked(false)
{
}

CCheck::CCheck(int_32 iX, int_32 iY, int_32 iWidth, int_32 iHeight)
	: CButtonControl(iX, iY, iWidth, iHeight), 
	m_bChecked(false)
{
}
CCheck::~CCheck()
{

}

const COID & CCheck::GetOID() const
{
	return OID_CHECK;
}

void CCheck::MouseClick(CMouseArgument & args)
{
	if(args.m_eButton == MouseButtonLeft)
		SetChecked(!m_bChecked);
	CButtonControl::MouseClick(args);
}

void CCheck::OnPaintBack(IPaint * pPaint, const CRectInt32 & rcClip) const
{
	//CRectInt32 rect = GetRect();
	//pPaint->DrawRect(rect.X, rect.Y, rect.Width - 1, rect.Height - 1, 0xFFFF0000, 1.0f);
}


void CCheck::SetChecked(bool bChecked)
{
	if(bChecked != m_bChecked)
	{
		m_bChecked = bChecked;
		Repaint();
	}
}

bool CCheck::IsChecked() const
{
	return m_bChecked;
}

void CCheck::DrawButtonIcon(IPaint * pPaint, int_32 iX, int_32 iY) const
{
	VerifyPointer(m_pIcon);
	switch(m_eIconTileMode)
	{
	case ImageTileModeV8:
		DrawIconV8(pPaint, iX, iY);
		break;
	default:
		throw CBadStateException();
	}
}
void CCheck::DrawButtonText(IPaint * pPaint, int_32 iX, int_32 iY) const
{
	pPaint->DrawString(m_text, m_text.GetLength(), iX, iY, &CSolidBrush(m_uiForeColor));
}

void CCheck::DrawIconV8(IPaint * pPaint, int_32 iX, int_32 iY) const
{
	VerifyPointer(m_pIcon);
	int_32 iTileW = m_pIcon->GetWidth();
	int_32 iTileH = m_pIcon->GetHeight() >> 3;
	int_32 iTileY = 0;

	if(m_bDisabled)
		iTileY = iTileH * 3;
	else if(m_bMouseDownL)
		iTileY = iTileH * 2;
	else if(m_bMouseHoving)
		iTileY = iTileH;
	else
		iTileY = 0;

	pPaint->DrawImage(m_pIcon,
		CRectFloat32((float_32)iX, (float_32)iY, (float_32)iTileW, (float_32)iTileH),
		CRectFloat32((float_32)0, (float_32)iTileY, (float_32)iTileW, (float_32)iTileH));

	if(m_bChecked)
	{
		pPaint->DrawImage(m_pIcon,
			CRectFloat32((float_32)iX, (float_32)iY, (float_32)iTileW, (float_32)iTileH),
			CRectFloat32((float_32)0, (float_32)iTileY + iTileH * 4, (float_32)iTileW, (float_32)iTileH));
	}
}
NAMESPACE_CORE_END
